﻿/*
COPYRIGHT (C) 2011-2013 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Web.Mvc;
using ScrumPilot.Domain.BPL;
using ScrumPilot.Helpers;
using Task = System.Threading.Tasks.Task;
using userimage = ScrumPilot.Domain.userimage;

namespace ScrumPilot.Controllers
{
    public class AsyncDataController : AsyncBaseController
    {

        #region Images
        // todo : simplify with new MVC 4 async meca 
        public void ShowPhoto16Async(Int32 id)
        {
            this.ShowPhoto(id, 16);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ImageResult ShowPhoto16Completed(ImageResult img)
        {
            return img;
        }

        public void ShowPhoto32Async(Int32 id)
        {
            this.ShowPhoto(id, 32);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ImageResult ShowPhoto32Completed(ImageResult img)
        {
            return img;
        }

        public void ShowPhoto64Async(Int32 id)
        {
            this.ShowPhoto(id, 64);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ImageResult ShowPhoto64Completed(ImageResult img)
        {
            return img;
        }

        public void ShowPhoto96Async(Int32 id)
        {
            this.ShowPhoto(id, 96);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ImageResult ShowPhoto96Completed(ImageResult img)
        {
            return img;
        }

        private void ShowPhoto(Int32 id, int size)
        {
            AsyncManager.OutstandingOperations.Increment();
            Task.Factory.StartNew(() =>
                                      {
                                          //This is my method for getting the image information
                                          // including the image byte array from the image column in
                                          // a database.
                                          userimage uim = (new UserServices(this.TFS)).GetUserImages(id);
                                          //As you can see the use is stupid simple.  Just get the image bytes and the
                                          //  saved content type.  See this is where the contentType comes in real handy.
                                          Byte[] img = null;
                                          if (uim != null)
                                          {
                                              switch (size)
                                              {
                                                  case 16:
                                                      img = uim.img16;
                                                      break;
                                                  case 32:
                                                      img = uim.img32;
                                                      break;
                                                  case 64:
                                                      img = uim.img64;
                                                      break;
                                                  case 96:
                                                      img = uim.img96;
                                                      break;
                                              }
                                          }
                                          AsyncManager.Parameters["img"] = uim != null
                                                                               ? new ImageResult(img, "image/png")
                                                                               : new ImageResult(
                                                                                     Url.Content(
                                                                                         String.Format(
                                                                                             "~/Content/img/h{0}x{0}.gif",
                                                                                             size)), "image/gif");
                                          AsyncManager.OutstandingOperations.Decrement();
                                      });
        }
 
        #endregion
    }
}